package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.service.HelloService;
import com.example.demo.util.HttpUtil;
import com.example.demo.vo.HelloDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;
    
    
    @PostMapping("/hello")
    public String hello(@RequestBody HelloDto dto) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            //header内的key不区分大小写
            //System.out.println(name + "=" + request.getHeader(name));
        }
        System.out.println(JSON.toJSONString(dto));
        return JSON.toJSONString(dto);
    }
    
    @PostMapping("/test")
    public String test(HttpServletResponse response, HttpServletRequest request, Model model, String name, @PathVariable(value = "yyls", required = false) String yyls) {
        System.out.println(request.getProtocol());
        return helloService.echo("test");
    }
    
    @GetMapping("/test2/**")
    public HelloDto test2(HttpServletResponse response, HttpServletRequest request) {
        log.info("test2");
        HelloDto helloDto = new HelloDto();
        helloDto.setName("hello");
        helloDto.setAge(18);
        return helloDto;
    }
    
    @GetMapping("/test3")
    public String test3() {
        String url = "https://alpha.wuuxiang.com/i5xforyou/diancan/gateway/keygen";
        ConcurrentHashMap<Long, Long> map = new ConcurrentHashMap<>();
        
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                Map<String, String> headerMap = new HashMap<>();
                headerMap.put("Tcsl-Shardingkey", "4412");
                headerMap.put("Authorization", "4412");
                headerMap.put("filterFlg", "4412");
                while (true) {
                    try {
                        JSONObject rst = HttpUtil.doPostJSONObject(url, "", headerMap);
                        
                        Long preValue = map.put(rst.getLong("result"), rst.getLong("result"));
                        if (preValue != null) {
                            log.info("出现重复，value=" + preValue);
                        }
                        
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }).start();
        }
        
        return "执行中";
    }
    
}
